<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>用户信息头部组件</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <style>
        /* 基础样式重置 */
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
        }

        /* 头部容器 */
        .header-container {
            width: 100%;
            background-color: #ffffff;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
            position: sticky;
            top: 0;
            z-index: 1000;
        }

        .header-content {
            max-width: 1200px;
            margin: 0 auto;
            padding: 0 20px;
            display: flex;
            justify-content: space-between;
            align-items: center;
            height: 70px;
        }

        /* Logo样式 */
        .logo {
            display: flex;
            align-items: center;
            gap: 10px;
        }

        .logo-icon {
            color: #4F46E5;
            font-size: 24px;
        }

        .logo-text {
            font-size: 1.5rem;
            font-weight: 600;
            color: #1E293B;
        }

        /* 导航链接 */
        .main-nav {
            display: flex;
            gap: 2rem;
        }

        .nav-link {
            text-decoration: none;
            color: #64748B;
            font-weight: 500;
            transition: all 0.2s ease;
            position: relative;
        }

        .nav-link:hover {
            color: #4F46E5;
        }

        .nav-link.active {
            color: #4F46E5;
        }

        .nav-link.active::after {
            content: '';
            position: absolute;
            bottom: -8px;
            left: 0;
            width: 100%;
            height: 3px;
            background-color: #4F46E5;
            border-radius: 3px;
        }

        /* 用户区域 */
        .user-section {
            display: flex;
            align-items: center;
            gap: 1.5rem;
        }

        /* 通知按钮 */
        .notification-btn {
            background: none;
            border: none;
            cursor: pointer;
            position: relative;
            color: #64748B;
            transition: color 0.2s ease;
        }

        .notification-btn:hover {
            color: #4F46E5;
        }

        .notification-badge {
            position: absolute;
            top: -5px;
            right: -5px;
            background-color: #EF4444;
            color: white;
            font-size: 10px;
            width: 16px;
            height: 16px;
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
        }

        /* 用户菜单 */
        .user-menu {
            position: relative;
        }

        .user-info {
            display: flex;
            align-items: center;
            gap: 10px;
            cursor: pointer;
        }

        .user-avatar {
            width: 40px;
            height: 40px;
            border-radius: 50%;
            object-cover: cover;
            border: 2px solid #F1F5F9;
            transition: transform 0.2s ease;
        }

        .user-info:hover .user-avatar {
            transform: scale(1.05);
        }

        .user-name {
            font-weight: 600;
            color: #1E293B;
            font-size: 14px;
        }

        .user-role {
            font-size: 12px;
            color: #94A3B8;
        }

        .user-menu .fa-chevron-down {
            color: #94A3B8;
            font-size: 12px;
            transition: transform 0.2s ease;
        }

        /* 下拉菜单 */
        .user-dropdown {
            position: absolute;
            right: 0;
            top: calc(100% + 10px);
            width: 200px;
            background-color: white;
            border-radius: 8px;
            box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
            padding: 8px 0;
            opacity: 0;
            visibility: hidden;
            transform: translateY(-10px);
            transition: all 0.2s ease;
            z-index: 1001;
        }

        .user-dropdown.show {
            opacity: 1;
            visibility: visible;
            transform: translateY(0);
        }

        .dropdown-item {
            display: flex;
            align-items: center;
            gap: 10px;
            padding: 10px 16px;
            color: #334155;
            text-decoration: none;
            transition: background-color 0.2s ease;
            font-size: 14px;
        }

        .dropdown-item:hover {
            background-color: #F8FAFC;
        }

        .dropdown-item i {
            width: 20px;
            color: #94A3B8;
        }

        .dropdown-item.logout {
            color: #EF4444;
        }

        .dropdown-item.logout i {
            color: #EF4444;
        }

        .dropdown-divider {
            height: 1px;
            background-color: #E2E8F0;
            margin: 8px 0;
        }

        /* 移动端菜单按钮 */
        .mobile-menu-btn {
            display: none;
            background: none;
            border: none;
            color: #1E293B;
            font-size: 24px;
            cursor: pointer;
        }

        /* 响应式设计 */
        @media (max-width: 768px) {
            .main-nav {
                display: none;
            }

            .mobile-menu-btn {
                display: block;
            }

            .user-info .user-details {
                display: none;
            }
        }
    </style>
</head>
<body>
    <header class="header-container">
        <div class="header-content">
            <!-- 左侧Logo -->
            <div class="logo">
                <span class="logo-icon">
                    <i class="fas fa-cube"></i>
                </span>
                <h1 class="logo-text">AppName</h1>
            </div>

            <!-- 导航链接 - 桌面端显示 -->
            <nav class="main-nav">
                <a href="#" class="nav-link active">首页</a>
                <a href="#" class="nav-link">探索</a>
                <a href="#" class="nav-link">消息</a>
                <a href="#" class="nav-link">帮助</a>
            </nav>

            <!-- 移动端菜单按钮 -->
            <button class="mobile-menu-btn">
                <i class="fas fa-bars"></i>
            </button>

            <!-- 用户信息区域 -->
            <div class="user-section">
                <!-- 通知图标 -->
                <button class="notification-btn" aria-label="通知">
                    <i class="fas fa-bell"></i>
                    <span class="notification-badge">3</span>
                </button>

                <!-- 用户信息和下拉菜单 -->
                <div class="user-menu">
                    <div class="user-info" id="userInfo">
                        <img 
                            src="https://picsum.photos/id/64/200/200" 
                            alt="用户头像" 
                            class="user-avatar"
                        >
                        <div class="user-details">
                            <p class="user-name">张明</p>
                            <p class="user-role">高级会员</p>
                        </div>
                        <i class="fas fa-chevron-down" id="dropdownArrow"></i>
                    </div>

                    <!-- 用户下拉菜单 -->
                    <div class="user-dropdown" id="userDropdown">
                        <a href="#" class="dropdown-item">
                            <i class="fas fa-user-circle"></i>
                            <span>个人资料</span>
                        </a>
                        <a href="#" class="dropdown-item">
                            <i class="fas fa-cog"></i>
                            <span>账户设置</span>
                        </a>
                        <a href="#" class="dropdown-item">
                            <i class="fas fa-credit-card"></i>
                            <span>会员中心</span>
                        </a>
                        <div class="dropdown-divider"></div>
                        <a href="#" class="dropdown-item logout">
                            <i class="fas fa-sign-out-alt"></i>
                            <span>退出登录</span>
                        </a>
                    </div>
                </div>
            </div>
        </div>
    </header>

    <script>
        // 用户下拉菜单交互
        const userInfo = document.getElementById('userInfo');
        const userDropdown = document.getElementById('userDropdown');
        const dropdownArrow = document.getElementById('dropdownArrow');
        const logout = document.querySelector('.logout');

        userInfo.addEventListener('click', () => {
            userDropdown.classList.toggle('show');
            dropdownArrow.classList.toggle('rotate-180');
        });

        // 点击其他区域关闭下拉菜单
        document.addEventListener('click', (e) => {
            if (!userInfo.contains(e.target)) {
                userDropdown.classList.remove('show');
                dropdownArrow.classList.remove('rotate-180');
            }
        });
        if(sessionStorage.getItem('Accesstoken')){
            // 如果拿到了token，则显示用户信息
            fetch('/api/users/profile',{
                method: 'post',
                headers: {
                    'Authorization': 'Bearer ' + sessionStorage.getItem('Accesstoken')
                }
            })
            .then(res => res.json())
            .then(data => {
                if(data.code === 200){
                    sessionStorage.setItem('Accesstoken', data.data.token)
                    document.querySelector('.user-name').innerText = data.data.username
                    document.querySelector('.user-role').innerText = data.data.email
                }else if(data.code === 402){
                    // refreshToken过期，需要重新登录
                    window.location.href = '/login'
                }
            })
        }

        logout.addEventListener('click', () => {
            fetch('/api/users/logout',{
                method: 'post',
            })
            .then(res => res.json())
            .then(data => {
                if(data.code === 200){
                    sessionStorage.removeItem('Accesstoken');
                }})
            });
    </script>
</body>
</html>
    